package com.hfy.music.service.impl;

import com.hfy.feign.IUserClient;
import com.hfy.model.Bo.SendUserNoticeBo;
import com.hfy.model.po.user.UserCommentPo;
import com.hfy.model.vo.CommentVo;
import com.hfy.model.vo.MusicVo;
import com.hfy.model.vo.NoticeVo;
import com.hfy.music.mapper.CommentMapper;
import com.hfy.music.mapper.DetailMusicMapper;
import com.hfy.music.mapper.NoticeMapper;
import com.hfy.music.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import result.Result;

import java.util.ArrayList;
import java.util.List;

/**
 * @ author 韩
 * time: 2024/9/25 15:10
 */

@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private DetailMusicMapper musicMapper;

    @Autowired
    private IUserClient userClient;

    @Override
    public Result getPersonalNotices(Long account) {
        List<SendUserNoticeBo> originalNotices = noticeMapper.getNoticesByAccount(account);
        ArrayList<NoticeVo> notices = new ArrayList<>();
        originalNotices.forEach(notice -> {
            NoticeVo noticeVo = new NoticeVo();
            // 1. 准备数据
            Integer noticeId = notice.getId();
            Integer commentId = notice.getCommentId();
            Integer musicId = notice.getMusicId();
            Integer doCommentId = notice.getDoCommentId();

            // 2. 开始SQL查询所需要填充的数据
            // 2.1 查询评论信息
            UserCommentPo userInfo = userClient.getInfoByAccount(account);
            CommentVo commentVo = commentMapper.queryInfoById(commentId);
            CommentVo doCommentVo = commentMapper.queryInfoById(doCommentId);
            MusicVo musicVo = musicMapper.queryMusicDetail(musicId);

            // 3. 进行判断
            if (userInfo == null) {
                noticeVo.setAvatar(null);
                noticeVo.setUsername("该用户已注销");
            } else {
                noticeVo.setAvatar(userInfo.getAvatar());
                noticeVo.setUsername(userInfo.getUsername());
            }
            if (doCommentVo.getDie()) {
                // 如果被评论的为空
                noticeVo.setComment("该评论已删除");
            } else {
                noticeVo.setComment(commentVo.getContent());
            }
            if (doCommentVo.getDie()) {
                noticeVo.setContent("该评论已删除");
                noticeVo.setTime(doCommentVo.getCommentTime());
            } else {
                noticeVo.setContent(doCommentVo.getContent());
                noticeVo.setTime(doCommentVo.getCommentTime());
            }

            // 4. 进行填充
            noticeVo.setId(noticeId);

            noticeVo.setAlbumCover(musicVo.getCover());
            noticeVo.setMusicName(musicVo.getName());
            noticeVo.setMusician(musicVo.getMusician());

            // 4. 添加到集合里
            notices.add(noticeVo);

        });

        return Result.ok(notices);
    }
}

